package com.example.commonlib.commonutil;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

import com.example.commonlib.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by aiyou on 2017/4/5.
 * 获取验证码计时器
 */

public class CodeTimeUtil {

    private String SMS_CODE_KEY;
    private int second;
    private Timer timer;
    private TimerTask timerTask;
    private Handler handler;

    private static CodeTimeUtil instance;

    public static CodeTimeUtil getInstance() {
        if (instance == null)
            instance = new CodeTimeUtil();

        return instance;
    }

    private CodeTimeUtil() {
    }

    @SuppressLint("HandlerLeak")
    public void startSmsCode(final TextView view, boolean isStartNow, String smsCode) {
        SMS_CODE_KEY = smsCode;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (view == null) {
                    return;
                }
                view.setText(msg.what + "秒可获取");
                view.setBackground(App.INSTANCE.getResources().getDrawable(R.drawable.shape_code_time_empty_bg));
                if (msg.what > 0) {
                    view.setEnabled(false);

                } else {
                    view.setEnabled(true);
                    view.setText(App.INSTANCE.getString(R.string.activity_get_send_code));
                    view.setBackground(App.INSTANCE.getResources().getDrawable(R.drawable.shape_code_time_bg));
                }

            }
        };


        String asString = ACache.get(App.INSTANCE).getAsString(SMS_CODE_KEY);
        long cacheTime = 0;
        if (asString != null) {
            cacheTime = Long.parseLong(asString);
        }
        if (System.currentTimeMillis() - cacheTime < 60 * 1000) {
            Toast.makeText(App.INSTANCE, "您一分钟内操作太频繁了哦!", Toast.LENGTH_SHORT).show();
        } else {
            if (isStartNow) {
                ACache.get(App.INSTANCE).put(SMS_CODE_KEY, System.currentTimeMillis() + "");
                if (view != null)
                    startTime(60);
            }
        }

    }

    public void startSmsCodeWithNoBackGround(final TextView view, boolean isStartNow, String smsCode) {
        SMS_CODE_KEY = smsCode;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (view == null) {
                    return;
                }
                view.setText(msg.what + "秒可获取");
                if (msg.what > 0) {
                    view.setEnabled(false);

                } else {
                    view.setEnabled(true);
                    view.setText(App.INSTANCE.getString(R.string.activity_get_send_code));
                }

            }
        };


        String asString = ACache.get(App.INSTANCE).getAsString(SMS_CODE_KEY);
        long cacheTime = 0;
        if (asString != null) {
            cacheTime = Long.parseLong(asString);
        }
        if (System.currentTimeMillis() - cacheTime < 60 * 1000) {
            Toast.makeText(App.INSTANCE, "您一分钟内操作太频繁了哦!", Toast.LENGTH_SHORT).show();
        } else {
            if (isStartNow) {
                ACache.get(App.INSTANCE).put(SMS_CODE_KEY, System.currentTimeMillis() + "");
                if (view != null)
                    startTime(60);
            }
        }

    }


    private void startTime(int s) {
        stopTimer();
        second = s;
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (second == 0) {
                    cancel();
                    return;
                }
                second--;
                handler.sendEmptyMessage(second);
            }
        };
        timer.schedule(timerTask, 0, 1000);

    }


    public void stopTimer() {
        if (timer != null) {
            timerTask = null;
            timer.cancel();
        }
    }


    public void removeCacheTime() {
        if (ACache.get(App.INSTANCE).getAsString(SMS_CODE_KEY) != null)
            ACache.get(App.INSTANCE).remove(SMS_CODE_KEY);
        stopTimer();
    }


    public void stopTimerType(TextView textView) {
        stopTimer();
        textView.setEnabled(true);
        textView.setText(App.INSTANCE.getString(R.string.activity_get_send_code));
        textView.setBackground(App.INSTANCE.getResources().getDrawable(R.drawable.shape_code_time_bg));
    }
}
